home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 2.toast / pc / sample code / printing / scriptable print simpletext / scriptableprinting.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-09-28  |  5.4 KB  |  184 lines

  1. /*
  2. **    File:        ScriptablePrinting.c
  3. **
  4. **    Functions defined in Technote 11xx
  5. **
  6. ** Copyright 1999 Apple Computer. All rights reserved.
  7. **
  8. **    You may incorporate this sample code into your applications without
  9. **    restriction, though the sample code has been provided "AS IS" and the
  10. **    responsibility for its operation is 100% yours. However, what you are
  11. **    not permitted to do is to redistribute the source as "DSC Sample Code"
  12. **    after having made changes. If you're going to re-distribute the source,
  13. **    we require that you make it clear in the source that the code was
  14. **    descended from Apple Sample Code, but that you've made changes.
  15. */
  16.  
  17. #include <Printing.h>
  18. #include <AppleEvents.h>
  19. #include "ScriptablePrinting.h"
  20. #include "ExtendPrintRecord.h"
  21. #include "CoercePrGeneral.h"
  22.  
  23. OSStatus getPrintRecordFromEvent(const AppleEvent *inAppleEvent,
  24.                                 THPrint        *hPrintP)
  25. /*    Given a print event Apple Event descriptor inAppleEvent,
  26.     look for the optional print setting parameter.
  27.     If this parameter exists, then coerce it into a print
  28.     record and place the handle to the print record into
  29.     *hPrintP. If the optional parameter does not exist
  30.     or it cannot be converted, then a non-zero error code
  31.     is returned and *hPrintP is set to NULL
  32. */
  33. {
  34.     OSStatus    err = noErr;
  35.     AEDesc        optionalDesc = {};
  36.     AEDesc        printRecordDesc = {};
  37.     
  38.     AECoercionHandlerUPP handler = NULL;
  39.     long refCon;
  40.     Boolean typeIsDesc;
  41.  
  42.     PrOpen();
  43.     err = PrError();
  44.  
  45.     if(err == noErr) {
  46.         err = AEGetParamDesc(inAppleEvent, keyAEPropData, typeAERecord, &optionalDesc);
  47.         if (err == noErr) {
  48.             // if you want to call PrGeneral directly, comment out this call,
  49.             // and set err to errAEHandlerNotFound. This will simulate the
  50.             // case when Desktop Printing is not running, so the coercion
  51.             // handler has not been loaded. See the Technote for more details.
  52.             err = AEGetCoercionHandler(typeAERecord, kPrintRecordAEType, 
  53.                                         &handler, &refCon, &typeIsDesc, true);
  54.  
  55.             if (err == noErr) {
  56.                 err = AECoerceDesc(&optionalDesc, kPrintRecordAEType, &printRecordDesc);
  57.             } else if (err == errAEHandlerNotFound) {
  58.                 /*    If desktop printing is not enabled,
  59.                     the handler won't be installed. This
  60.                     is not fatal, since we can call the
  61.                     driver's PrGeneral call directly.
  62.                     It's better to use the coercion handler,
  63.                     but if we can't find it, this is a
  64.                     good fallback to use
  65.                  */
  66.                 PrCoerceStruct    coerceData;
  67.                 
  68.                 coerceData.iOpCode = kPrCoerceOp;
  69.                 coerceData.iError = noErr;
  70.                 coerceData.lReserved = 0;
  71.                 coerceData.fromDesc = &optionalDesc;
  72.                 coerceData.toType = kPrintRecordAEType;
  73.                 coerceData.toDesc = &printRecordDesc;
  74.  
  75.                 PrGeneral((Ptr)&coerceData);
  76.  
  77.                 err = coerceData.iError;
  78.             }
  79.         }
  80.     }
  81.  
  82.     if (err == noErr) {
  83.         OSErr    tempErr;
  84.  
  85.         *hPrintP = (THPrint) printRecordDesc.dataHandle;
  86.         err = HandToHand((Handle *)hPrintP);
  87.         tempErr = AEDisposeDesc(&printRecordDesc);
  88.         if (err == noErr) err = tempErr;
  89.     } else {
  90.         *hPrintP = NULL;
  91.     }
  92.     
  93.     err = AEDisposeDesc(&optionalDesc);
  94.     
  95.     PrClose();
  96.  
  97.     return err;
  98. }
  99.  
  100. OSStatus getPrintJobPrintRec(THPrint docPrintRec,
  101.                             THPrint settingsPrintRec,
  102.                             THPrint *jobPrintRecP)
  103. /*    The caller passes in the print record stored with a
  104.     document, docPrintRec, as well as the print record
  105.     obtained from the print event, settingsPrintRec.
  106.     This function creates a new print record that combines
  107.     the formatting information from docPrintRec with
  108.     the print time settings in settingsPrintRec and
  109.     places the new print record into *jobPrintRecP.
  110.     On entry, settingsPrintRec can be NULL in which
  111.     case docPrintRec is duplicated and returned in
  112.     *jobPrintRecP. In either case, if a new print
  113.     record can not be created, this function sets
  114.     *jobPrintRecP to NULL and returns a non-zero error
  115.     code.
  116. */
  117. {
  118.     OSStatus    err = noErr;
  119.     THPrint        jobPrintRec = docPrintRec;
  120.     
  121.     err = HandToHand((Handle *)&jobPrintRec);
  122.     if ((err == noErr) && (settingsPrintRec != NULL)) {
  123.         /*    Both print records must be extended when
  124.             calling PrJobMerge in order to get a full
  125.             merge. The scripted print settings print
  126.             record is extended by the coercion so
  127.             nothing need be done to it here.
  128.         */
  129.         err = extendPrValidate(docPrintRec);    // from TN 1161
  130.         
  131.         if (err == noErr) {
  132.             PrJobMerge(settingsPrintRec, jobPrintRec);
  133.             err = PrError();
  134.         }
  135.     } else {
  136.         jobPrintRec = NULL;
  137.     }
  138.     
  139.     if (err != noErr) {
  140.         if (jobPrintRec != NULL) {
  141.             DisposeHandle((Handle) jobPrintRec);
  142.             jobPrintRec = NULL;
  143.         }
  144.     }
  145.     
  146.     *jobPrintRecP = jobPrintRec;
  147.     
  148.     return err;
  149. }
  150.  
  151. OSStatus getPrintJobShowDialog(const AppleEvent *inAppleEvent,
  152.                                 Boolean    *showDialog)
  153. /*    Given a print Apple Event, look for the optional parameter
  154.     saying whether or not the application should show the
  155.     PrJobDialog. While an apple event with the "print settings"
  156.     parameter COULD have most of the items that will be needed
  157.     to print the job, it's possible that the user or scripter
  158.     will     want to present the dialog to handle printer-specific
  159.     settings. An application should not prevent the user from
  160.     doing so, so this function is needed. The application should
  161.     default to showing the dialog if the parameter is not
  162.     specified.
  163. */
  164. {
  165.     OSStatus    err = noErr;
  166.     AEDesc        showDesc = {};
  167.     
  168.     err = AEGetParamDesc(inAppleEvent, kPrintDialogAEType,
  169.                         typeBoolean, &showDesc);
  170.     if (err == noErr) {
  171.         *showDialog = **(showDesc.dataHandle);
  172.     }
  173.     if (err == errAEDescNotFound) {
  174.         /*    not having the descriptor is okay.
  175.             It just means we default to showing.
  176.         */
  177.         *showDialog = true;
  178.         err = noErr;
  179.     }
  180.     
  181.     return err;
  182. }
  183.  
  184.